package cn.gov.pbc.tims.controller;

import java.util.List;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;

import cn.gov.pbc.tims.entity.Jginfo;
import cn.gov.pbc.tims.entity.City;
import cn.gov.pbc.tims.entity.User;
import cn.gov.pbc.tims.service.CommonService;
import cn.gov.pbc.tims.service.DictService;
import cn.gov.pbc.tims.util.Operator;
import cn.gov.pbc.tims.util.PageData;
import cn.gov.pbc.tims.util.Result;
  

@Controller  
@RequestMapping("/jg2")  
public class JgAllController2 {  
  
    @Autowired  
    private CommonService commonService;
    @Autowired 
    private DictService systemService;
    @Autowired
    private Operator operator;    
	@Autowired 
	private ObjectMapper objectMapper;

    @Autowired
	private SessionFactory sessionFactory;

	private Session getCurrentSession() {
		return this.sessionFactory.getCurrentSession();
	}

	@Autowired
	private PageData grid_data;
    
 
    
    @RequestMapping("/getJgAll") 
    public @ResponseBody PageData getJgAll(Integer page, Integer rows, String filter) {
    	User oper=operator.getUser();
    	//System.out.println("page="+page+"page size="+rows+"   filter="+filter);
    	//二级缓存，后面加条件，只取一部分结果
    	if(filter == null) filter="";
    	
		Query<Jginfo> q = this.getCurrentSession().createQuery("from Jginfo where c1Id='C' and pbcId='"+oper.getJgId()+"' "+filter, Jginfo.class).setCacheable(true);
		q.setFirstResult((page-1)*rows).setMaxResults(rows);
		List<Jginfo> hbJginfos = q.getResultList();
		
		//取出全部的条目数，为了计算表格页数
		Query<Long> q2 = this.getCurrentSession().createQuery("select count(1) from HbJginfo where c1Id='C' and pbcId='"+oper.getJgId()+"' "+filter, Long.class).setCacheable(true);
		Long count = q2.getSingleResult();
		
		grid_data.setValue(count.intValue(), hbJginfos);
		return grid_data;
    }  
    
   
    
    @RequestMapping("/deletebyid")  
	public @ResponseBody Result  login(int iid){  
    	Result result=new Result();
    	//System.out.println("传来的iid=="+iid);
		
    	//CommonService.delete(iid);
    	
		result.setValue(0, "删除成功");
		
		return result;  
	}  
       
    @RequestMapping("savejg")
	public @ResponseBody Result savejg(Jginfo op){
    	Result result=new Result();
    	
    		User oper=operator.getUser();
		
		 
    	
    	
    	try {
    		commonService.saveOrUpdate(op);
    		result.setValue(0, "保存成功！");
		} catch (Exception e) {
			result.setValue(0, "该金融机构代码已存在或格式错误，请重新录入！");
		}
		
		

		return result;		
	}
    
    
    
   

}  
